package m.meiweitech.com.mwsp

import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job
import m.meiweitech.com.demo.databinding.FragmentSplashBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.countDownCoroutines
import www.wen.com.common.lib.extension.resetStartDestination
import www.wen.com.common.lib.util.LocalInfoUtil


/**
 * @author WEN
 * @Description:启动页面
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class SplashFragment : BaseLoadingFragment<FragmentSplashBinding>(), View.OnClickListener {

    private lateinit var mJob: Job

    private val mBaseApiViewModel: SplashViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun getLayoutId(): Int = R.layout.fragment_splash

    override fun lazyInitView() {
        mJob = countDownCoroutines(
            total = 2,
            onTick = {
                "跳过$it".also { count -> dataBinding.btnCountdown.text = count }
            },
            onFinish = {
                jumpNextScreen()
            },
            scope = lifecycleScope
        )
    }

    override fun onClick(v: View) {
        when(v.id){

            R.id.btnCountdown->{
                jumpNextScreen()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (this::mJob.isInitialized) {
            mJob.cancel()
        }
    }

    private fun jumpNextScreen() {
        requireActivity().resetStartDestination(R.navigation.navigation, R.id.mainFragment)
    }

}



